package owl2vowl.parser.vowl;

import owl2vowl.constants.OntologyInformationEnum;
import owl2vowl.model.annotation.Annotation;
import owl2vowl.model.data.VowlData;
import owl2vowl.model.ontology.OntologyInformation;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class OntologyInformationParser {
	private VowlData vowlData;
	private OWLOntology ontology;

	public OntologyInformationParser(VowlData vowlData, OWLOntology ontology) {
		this.vowlData = vowlData;
		this.ontology = ontology;
	}

	public void execute() {
		Set ontologyAnnotations = (new AnnotationVisitor(this.vowlData, (OWLOntologyManager) null))
				.getOntologyAnnotations(this.ontology);
		OntologyInformation ontologyInformation = this.vowlData.getOntologyInformation();
		ontologyInformation.getAnnotations().fillAnnotations(ontologyAnnotations);
		Optional ontologyIRI = this.ontology.getOntologyID().getOntologyIRI();
		if (ontologyIRI.isPresent()) {
			ontologyInformation.setIri(((IRI) ontologyIRI.get()).toString());
		} else {
			ontologyInformation.setIri("No IRI set");
		}

		Map identifierMap = ontologyInformation.getAnnotations().getIdentifierToAnnotation();
		if (identifierMap.containsKey(OntologyInformationEnum.VERSION.getValue())) {
			ontologyInformation.setVersion(
					((Annotation) ((List) identifierMap.get(OntologyInformationEnum.VERSION.getValue())).get(0))
							.getValue());
		}

		if (identifierMap.containsKey(OntologyInformationEnum.TITLE.getValue())) {
			ontologyInformation.setTitles((List) identifierMap.get(OntologyInformationEnum.TITLE.getValue()));
		}

		if (identifierMap.containsKey(OntologyInformationEnum.AUTHOR.getValue())) {
			((List) identifierMap.get(OntologyInformationEnum.AUTHOR.getValue())).forEach((annotation) -> {
				ontologyInformation.addAuthor(((Annotation) annotation).getValue());
			});
		}

	}
}